﻿#ifndef STE_CONTROLLER_H
#define STE_CONTROLLER_H

#include <QMainWindow>
#include <QSerialPort>
#include <QSerialPortInfo>

#include <QMap>
#include <QList>
#include <QMessageBox>
#include <QDateTime>
#include <QDate>
#include <QTime>
#include <QDateTime>
#include <QTreeWidgetItem>
namespace Ui {
class STE_Controller;
}

typedef struct
{
  quint8 header;
  quint8 type;
  quint8 devicetype;
  quint8 devicenum;
  quint8 remotetype;
  quint8 remotenum;
  quint8 datacode;
  quint8 data[2];
  quint8 sum;
}ZigbeeDataType;

typedef union
{
    ZigbeeDataType data;
    quint8 buf[10];
}ZigbeeRecDataType;

class STE_Controller : public QMainWindow
{
    Q_OBJECT

public:
    explicit STE_Controller(QWidget *parent = nullptr);
    ~STE_Controller();

private slots:
    void on_btn_refresh_clicked();
    void on_btn_link_clicked();
    void receiveInfo();
    void on_btn_send_clicked();

    void on_btn_clear_clicked();

    void on_btn_start_clicked();

    void on_btn_stop_clicked();

    void on_btn_enum_clicked();

    void on_button_typechange_clicked();

    void on_button_test_clicked();

    void on_treeWidget_devicelist_itemClicked(QTreeWidgetItem *item, int column);

private:
    Ui::STE_Controller *ui;
    QSerialPort *serialPort;
    QList<QSerialPortInfo> ports;
    ZigbeeRecDataType zigbee_data;
    QMap <quint8,QString> devicemap;

    bool zigbee_Calsum(ZigbeeRecDataType &chk);
    void zigbee_infodisp(ZigbeeRecDataType &frame);
    void findFreePorts(void);
    void zigbee_handler(quint8 ch);
    void devicestringmapinit();
    QString devicetostring(quint8 type,quint8 num);
    QString eventtostring(ZigbeeRecDataType &frame);
    QString parttostring(quint8 part);
    void calChecksum(quint8 data[]);
};

#endif // STE_CONTROLLER_H
